<?php
class RegionAction extends CommonAction {
    public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Region');
    }
   
    public function index(){
		$info = array();
		$info['pid'] = I('pid', 0, 'intval');
		//获得列表
		$list = $this->modelObject->getAll(array('pid'=>$info['pid']), 'id ASC');
		$listCount = count($list);
		if($listCount < 4){
			$list = array_pad($list, 4, array());	
		}else if($listCountCeil = ceil($listCount / 4) * 4){
			
			$list = array_pad($list, $listCountCeil, array());	
		}
		
		$this->assign('list', $list);
		
		
		if($info['pid'] > 0){
			$info['types'] = $this->modelObject->where(array('id'=>$info['pid']))->getField('types');
			$info['types']++;
		}else{
			$info['types'] = 0;
		}
		switch($info['types']){
			case 0:
				$info['region'] = '一级';
			break;
			case 1:
				$info['region'] = '二级';
			break;
			case 2:
				$info['region'] = '三级';
			break;	
		}
		
		$this->assign('info', $info);
		$this->display();
    }
	
	public function add(){
		$info = array();
		$info['pid'] = I('pid', 0, 'intval');
		if(IS_POST){
			if($this->modelObject->create()){
				if($this->modelObject->add()){
					F('province', NULL);
					F('city_'.$info['pid'], NULL);
					
					$this->success('插入数据成功', U('index', array('pid'=>$info['pid'])));	
				}else{
					$this->error('插入失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			if($info['pid'] > 0){
				$info['types'] = $this->modelObject->where(array('id'=>$info['pid']))->getField('types');
				$info['types'] ++ ;
				
				switch($info['types']){
					case 0:
						$info['region'] = '一级';
					break;
					case 1:
						$info['region'] = '二级';
					break;
					case 2:
						$info['region'] = '三级';
					break;	
				}
			}else{
				$info['types'] = 0;
				$info['region'] = '一级';			
			}
			
			$this->assign('info', $info);
			$this->display();	
		}
	}
	
	public function edit(){
		if(IS_POST){
			if($this->modelObject->create()){
				if($this->modelObject->save()){
					$pid = I('pid',0,'intval');
					F('province', NULL);
					F('city_'.$pid, NULL);
					
					$this->success('编辑成功', U('index', array('pid'=>$pid)));	
				}else{
					$this->error('编辑失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$id = I('id', 0, 'intval');
			if($id){
				$info = $this->modelObject->getInfo($id);
				
				if($info['pid'] > 0){
					$info['types'] = $this->modelObject->where(array('id'=>$info['pid']))->getField('types');
					$info['types'] ++ ;
					
					switch($info['types']){
						case 0:
							$info['region'] = '一级';
						break;
						case 1:
							$info['region'] = '二级';
						break;
						case 2:
							$info['region'] = '三级';
						break;	
					}
				}else{
					$info['types'] = 0;
					$info['region'] = '一级';			
				}
			
				$this->assign('info', $info);
				$this->display();	
			}else{
				$this->error('参数错误!');
			}
		}
	}
	
	public function del(){
		$id = I('id', 0, 'intval');
		if($id){
			$info = $this->modelObject->getInfo($id);
			$ids[0] = $id;
			if($info['types'] == 0){
				$ids[0] = $id;
				if($ids[1] = $this->modelObject->where(array('pid'=>$id))->getField('group_concat(id) as c')){
					$ids[2] = $this->modelObject->where(array('pid'=>array('IN', $ids[1])))->getField('group_concat(id) as c');		
				}
				
			}else if($info['types'] == 1){
				$ids[1] = $this->modelObject->where(array('pid'=>$id))->getField('group_concat(id) as c');	
			}
			$idsStr = implode(',', $ids);
			
			if($this->modelObject->deleteInfo(array('id'=>array('IN', $idsStr)))){
				$this->success('删除成功', U('index'));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	//获得所有省份
	public function getProvince(){
		$province = array();
		if(!($province = F('province'))){
			$province = $this->modelObject->getAll(array('types'=>0), 'id ASC', 'id,name');
			F('province', $province);
		}
		
		return $province;
	}
	
	public function getCity($provinceId = 0){
		if($provinceId){
			if(!($city = F('city_'.$provinceId))){
				$city = $this->modelObject->getAll(array('pid'=>$provinceId, 'types'=>1), 'id ASC','id,name');
				F('city_'.$provinceId, $city);
			}
			return $city;
		}
		return false;	
	}
	
	public function ajaxGetCity(){
		if($provinceId = I('province_id', 0 , 'intval')){
			$city = $this->getCity($provinceId);
			$this->ajaxReturn($city, '城市列表', 1);	
		}else{
			$this->ajaxReturn(0, '返回数据失败', 0);	
		}
	}
}//end class